var app = angular.module('ZJPC');
app.controller('TrackPerFileCtrl', ['$scope','$state','$rootScope','$stateParams','DataService','$filter',TrackPerFileCtrl]);

function TrackPerFileCtrl($scope,$state,$rootScope,$stateParams,DataService,$filter) {
	
	$scope.carFile = {name:$stateParams.xm+'('+$stateParams.jid+')'}

	$scope.pageOption = {
		pageSize:10,
		itemCount:0,
		current:0
	}

	$scope.getData = function(item){
		console.log($scope.pageOption)
		search();
	}

	function search(){
		$scope.loading = true;
		DataService.getPersonTrackByPagin($filter('date')($scope.starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')($scope.endtime,'yyyy-MM-dd HH:mm:ss'),$scope.pageOption.current,$scope.pageOption.pageSize,$stateParams.jid,function(data){
			$scope.loading = false;
			$scope.pageOption.itemCount = data.total;
			$scope.list = data.data;
			$scope.$broadcast('list',{list:$scope.list,pageOption:$scope.pageOption})
		})
	}
	
	$scope.searchTrack = function(){
		search();
		$scope.pageOption.current = 0;
	}

	$scope.endtime = new Date();
	$scope.starttime = new Date().setHours(0,0,0);
	$scope.searchTrack();

	$scope.chosen = 'list'
	$state.go('app.file.perfile.track.list',{list:$scope.list})

	$scope.clickType = function(a){
		if(a =='list'){
			$scope.chosen='list'
		}else{
			$scope.chosen = 'pic'
		}
	}

		/*时间控件校验相关设置*/
	$scope.endDateBeforeRender = endDateBeforeRender
	$scope.endDateOnSetTime = endDateOnSetTime
	$scope.startDateBeforeRender = startDateBeforeRender
	$scope.startDateOnSetTime = startDateOnSetTime

	function startDateOnSetTime () {
	  $scope.$broadcast('start-date-changed');
	}

	function endDateOnSetTime () {
	  $scope.$broadcast('end-date-changed');
	}

	function startDateBeforeRender ($dates) {
	  if ($scope.endtime) {
	    $scope.endtime = new Date($scope.endtime).setHours(23,59,59)
	    var activeDate = moment($scope.endtime);

	    $dates.filter(function (date) {
	      return date.localDateValue() >= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}

	function endDateBeforeRender ($view, $dates) {
	  if ($scope.starttime) {
	    var activeDate = moment($scope.starttime).subtract(1, $view).add(1, 'minute');

	    $dates.filter(function (date) {
	      return date.localDateValue() <= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}

}